#include "TextureUnitsGL3x.h"

using namespace Renderer;
using namespace Renderer::GL3x;


void Renderer::GL3x::TextureUnitsGL3x::NotifyDirty(ICleanable* value)
{
	_dirtyTextureUnits.push_back(value);
}

void Renderer::GL3x::TextureUnitsGL3x::Clean()
{
	for (int i = 0; i < _dirtyTextureUnits.size(); ++i)
	{
		_dirtyTextureUnits[i]->Clean();
	}
	_dirtyTextureUnits.clear();
	_lastTextureUnit->CleanLastTextureUnit();
}

int Renderer::GL3x::TextureUnitsGL3x::GetCount()
{
	return _textureUnits.size();
}

void Renderer::GL3x::TextureUnitsGL3x::SetTextureUnitAt(int index, TextureUnit* textureUnit)
{
	_textureUnits[index] = textureUnit;
}

TextureUnit* Renderer::GL3x::TextureUnitsGL3x::GetTextureUnitAt(int index) const
{
	return _textureUnits[index];
}

Renderer::GL3x::TextureUnitsGL3x::TextureUnitsGL3x()
{
	//
	// Device.NumberOfTextureUnits is not initialized yet.
	//
	int numberOfTextureUnits;
	glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numberOfTextureUnits);

	_textureUnits = std::vector<TextureUnit*>(numberOfTextureUnits, 0);
	for (int i = 0; i < numberOfTextureUnits; i++)
	{
		TextureUnitGL3x* textureUnit = new TextureUnitGL3x(i, this);
		_textureUnits[i] = textureUnit;
	}

	_dirtyTextureUnits = std::vector<ICleanable*>();
	_lastTextureUnit = (TextureUnitGL3x*)_textureUnits[numberOfTextureUnits - 1];
}
